<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <script type="text/javascript" src="./js/jquery-1.12.4.min.js"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', 'Helvetica Neue', Arial, sans-serif;
        }

        body {
            background: #f8f9fa;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
            background-image: linear-gradient(120deg, #fdfbfb 0%, #f5f7fa 100%);
        }

        .login-container {
            background: white;
            border-radius: 16px;
            box-shadow: 0 10px 30px rgba(0, 0, 0, 0.05);
            width: 100%;
            max-width: 420px;
            padding: 50px 40px;
            text-align: center;
            position: relative;
            overflow: hidden;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .login-container:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.08);
        }

        .logo {
            color: #4a6cf7;
            font-size: 3.8rem;
            margin-bottom: 15px;
            text-shadow: 0 2px 4px rgba(74, 108, 247, 0.1);
        }

        h1 {
            color: #2d3748;
            font-weight: 600;
            margin-bottom: 8px;
            font-size: 1.9rem;
            letter-spacing: 0.5px;
        }

        .tagline {
            color: #718096;
            margin-bottom: 35px;
            font-size: 1rem;
            font-weight: 400;
        }

        .input-group {
            position: relative;
            margin-bottom: 25px;
        }

        input {
            width: 100%;
            padding: 16px 16px 16px 52px;
            border: 1px solid #e2e8f0;
            border-radius: 12px;
            font-size: 1rem;
            transition: all 0.3s ease;
            outline: none;
            background: #f8fafc;
            color: #2d3748;
        }

        input::placeholder {
            color: #a0aec0;
        }

        input:focus {
            border-color: #4a6cf7;
            box-shadow: 0 0 0 3px rgba(74, 108, 247, 0.15);
            background: #ffffff;
        }

        .input-icon {
            position: absolute;
            left: 20px;
            top: 50%;
            transform: translateY(-50%);
            color: #a0aec0;
            font-size: 1.1rem;
            transition: color 0.3s ease;
        }

        input:focus + .input-icon {
            color: #4a6cf7;
        }

        .btn-login {
            background: linear-gradient(to right, #4a6cf7, #6c8bff);
            color: white;
            border: none;
            border-radius: 12px;
            padding: 16px;
            width: 100%;
            font-size: 1.05rem;
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
            margin-top: 10px;
            letter-spacing: 0.5px;
            box-shadow: 0 4px 15px rgba(74, 108, 247, 0.25);
            position: relative;
        }

        .btn-login:hover:not(:disabled) {
            transform: translateY(-3px);
            box-shadow: 0 6px 20px rgba(74, 108, 247, 0.35);
        }

        .btn-login:active {
            transform: translateY(0);
        }

        .btn-login:disabled {
            opacity: 0.8;
            cursor: not-allowed;
        }

        .btn-loading .spinner {
            display: inline-block;
            width: 20px;
            height: 20px;
            border: 3px solid rgba(255,255,255,0.3);
            border-radius: 50%;
            border-top-color: #fff;
            animation: spin 1s ease-in-out infinite;
            margin-right: 10px;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .options {
            display: flex;
            justify-content: space-between;
            margin-top: 22px;
            font-size: 0.92rem;
        }

        .options label {
            color: #4a5568;
            display: flex;
            align-items: center;
            gap: 8px;
            cursor: pointer;
        }

        .options input[type="checkbox"] {
            width: 16px;
            height: 16px;
            padding: 0;
            accent-color: #4a6cf7;
            cursor: pointer;
        }

        a {
            color: #4a6cf7;
            text-decoration: none;
            transition: all 0.3s ease;
            font-weight: 500;
        }

        a:hover {
            color: #2b50e0;
            text-decoration: underline;
        }

        .divider {
            margin: 30px 0;
            position: relative;
            height: 1px;
            background: linear-gradient(to right, transparent, #e2e8f0, transparent);
        }

        .divider::after {
            content: "";
            position: absolute;
            top: -10px;
            left: 50%;
            transform: translateX(-50%);
            background: white;
            padding: 0 15px;
            color: #718096;
            font-size: 0.85rem;
        }

        .signup {
            margin-top: 20px;
            color: #718096;
            font-size: 0.95rem;
        }

        .decoration {
            position: absolute;
            z-index: -1;
            opacity: 0.1;
        }

        .decoration-1 {
            top: -50px;
            right: -50px;
            width: 200px;
            height: 200px;
            border-radius: 50%;
            background: linear-gradient(45deg, #4a6cf7, #6c8bff);
        }

        .decoration-2 {
            bottom: -80px;
            left: -50px;
            width: 150px;
            height: 150px;
            border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
            background: linear-gradient(45deg, #4a6cf7, #6c8bff);
        }

        .decoration-3 {
            top: 50%;
            left: -30px;
            width: 80px;
            height: 80px;
            border-radius: 50%;
            background: linear-gradient(45deg, #4a6cf7, #6c8bff);
        }

        @media (max-width: 480px) {
            .login-container {
                padding: 40px 25px;
            }

            .logo {
                font-size: 3.2rem;
            }

            h1 {
                font-size: 1.7rem;
            }

            .social-btn {
                width: 48px;
                height: 48px;
            }
        }

        .footer {
            position: absolute;
            bottom: 20px;
            width: 100%;
            text-align: center;
            color: #a0aec0;
            font-size: 0.85rem;
        }

        .alert {
            padding: 12px 16px;
            border-radius: 8px;
            margin-bottom: 20px;
            display: none;
        }

        .alert-error {
            background-color: #fee;
            color: #c33;
            border: 1px solid #fcc;
        }

        .alert-success {
            background-color: #efe;
            color: #363;
            border: 1px solid #cfc;
        }
    </style>
</head>
<body>
<div class="login-container">
    <!-- 装饰元素 -->
    <div class="decoration decoration-1"></div>
    <div class="decoration decoration-2"></div>
    <div class="decoration decoration-3"></div>

    <div class="logo">
        <i class="fas fa-user-circle"></i>
    </div>
    <h1>欢迎回来</h1>
    <p class="tagline">请登录您的账户以继续</p>

    <div id="loginError" class="alert alert-error"></div>
    <div id="loginSuccess" class="alert alert-success"></div>

    <form id="loginForm">
        <div class="input-group">
            <input id="username" type="text" name="username" placeholder="用户名或邮箱" required>
            <i class="fas fa-user input-icon"></i>
        </div>

        <div class="input-group">
            <input id="password" type="password" name="password" placeholder="密码" required>
            <i class="fas fa-lock input-icon"></i>
        </div>

        <button type="submit" class="btn-login" id="loginButton">
            <span class="button-text">登 录</span>
        </button>

        <div class="options">
            <label>
                <input type="checkbox"> 记住我
            </label>
            <a href="#">忘记密码</a>
        </div>

        <div class="divider"></div>

        <div class="signup">
            还没有账号? <a href="loginUser">立即注册</a>
        </div>
    </form>
</div>

<div class="footer">
    © 2023 简约登录系统 | 隐私政策 | 使用条款
</div>

<script>
    // 输入框交互效果
    document.querySelectorAll('input').forEach(input => {
        input.addEventListener('focus', function() {
            this.parentElement.classList.add('focused');
        });

        input.addEventListener('blur', function() {
            this.parentElement.classList.remove('focused');
        });
    });

    // 表单提交处理
    document.getElementById('loginForm').addEventListener('submit', function(e) {
        e.preventDefault();
        toAuth();
    });

    function showError(message) {
        const errorEl = document.getElementById('loginError');
        errorEl.textContent = message;
        errorEl.style.display = 'block';
        document.getElementById('loginSuccess').style.display = 'none';
    }

    function showSuccess(message) {
        const successEl = document.getElementById('loginSuccess');
        successEl.textContent = message;
        successEl.style.display = 'block';
        document.getElementById('loginError').style.display = 'none';
    }

    function setLoadingState(isLoading) {
        const button = document.getElementById('loginButton');
        if (isLoading) {
            button.disabled = true;
            button.classList.add('btn-loading');
            button.innerHTML = '<span class="spinner"></span> 登录中...';
        } else {
            button.disabled = false;
            button.classList.remove('btn-loading');
            button.innerHTML = '<span class="button-text">登 录</span>';
        }
    }

    function toAuth() {
        const username = document.getElementById('username').value;
        const password = document.getElementById('password').value;

        // 验证输入
        if (!username || !password) {
            showError('请输入用户名和密码');
            return;
        }

        // 显示加载状态
        setLoadingState(true);
        hideAlerts();

        let data = {
            username: username,
            password: password
        };

        $.ajax({
            url: "/auth/login",
            type: "POST",
            data: data,
            success: function() {
                console.log('登录成功！');
                showSuccess('登录成功，正在跳转...');

                // 获取Thymeleaf传递的参数
                let redirectUrl = 'http://localhost:9092/base/oauth/callback';
                let responseType = 'code';
                let clientId = 'ali_client';
                let scope = 'customer';
                let state = 'xyz123abc';

                // 等待一小段时间确保登录状态已保存，然后重定向
                setTimeout(function() {
                    window.location.href = "http://localhost:9094/auth/oauth/authorize?response_type=" + responseType +
                        "&client_id=" + clientId + "&redirect_uri=" + encodeURIComponent(redirectUrl) +
                        "&scope=" + scope + "&state=" + state;
                }, 1000);
            },
            error: function(xhr, status, error) {
                console.log('登录失败：', error);
                setLoadingState(false);

                if (xhr.status === 401) {
                    showError('用户名或密码错误');
                } else if (xhr.status === 0) {
                    showError('网络错误，请检查网络连接');
                } else {
                    showError('登录失败: ' + (xhr.responseText || error));
                }
            }
        });
    }

    function hideAlerts() {
        document.getElementById('loginError').style.display = 'none';
        document.getElementById('loginSuccess').style.display = 'none';
    }

    // 页面加载时隐藏警告信息
    document.addEventListener('DOMContentLoaded', hideAlerts);
</script>
</body>
</html>
